package cn.parus.controller;

import cn.parus.common.Execption.CustomException;
import cn.parus.common.Execption.Result;
import cn.parus.entity.FileMetadata;
import cn.parus.service.FileMetadataService;
import cn.parus.service.HdfsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.IOException;


/**
 * @Author: Axel
 * @Description: 核心文件管理相关接口
 **/
@Slf4j
@RestController
@RequestMapping("/file-core")
public class FileCoreController {

    @Autowired
    private FileMetadataService fileMetadataService;

    /**
     * 查询文件元数据
     */
    @GetMapping("/search-all")
    public Result searchAllFileMetadata(
            @RequestParam(value = "filePath", required = false) String filePath,
            @RequestParam(value = "ownerId", required = true) String ownerId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(value = "fileType", required = false) String fileType,
            @RequestParam(defaultValue = "1") String status) {
        try {
            Page<FileMetadata> fileMetadata = fileMetadataService.searchFileMetadata(filePath, ownerId, current, size, status, fileType);
            return Result.success(fileMetadata);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 生成新的文件夹
     */
    @PostMapping("/create-new-folder")
    public Result createNewFolder(@RequestBody FileMetadata fileMetadata) {
        try {
            fileMetadataService.createNewFolder(fileMetadata);
            return Result.success("创建成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新建文件（非文件夹）
     */
    @Deprecated
    @PostMapping("/create-new-file")
    public Result createNewFile(@RequestBody FileMetadata fileMetadata) {
        try {
            // 设置文件类型为非目录
            fileMetadata.setIsDirectory(false);
            fileMetadataService.createNewFile(fileMetadata);
            return Result.success("文件创建成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除文件夹、文件
     */
    @DeleteMapping("/delete-document")
    public Result deleteDocument(@RequestParam("fileId") String fileId) {
        try {
            fileMetadataService.deleteDocument(fileId);
            return Result.success("删除成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改文件、文件夹名称
     * @param fileMetadata
     * @return
     */
    @PutMapping("/update-document")
    public Result updateDocument(@RequestBody FileMetadata fileMetadata) {
        try {
            fileMetadataService.updateDocument(fileMetadata);
            return Result.success("更新成功");
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    @Autowired
    private HdfsService hdfsService;
    // TODO 不能直接接上hadoop的接口，必须要先经过后端的服务

    /**
     * 文件上传，文件上传至hdfs后再写入数据库
     * @param fileMetadata
     * @return
     * @throws IOException
     */
    @PutMapping("/upload-file-to-hdfs")
    public Result uploadFileToHDFS(@RequestBody FileMetadata fileMetadata) throws IOException {
        log.info(fileMetadata.toString());
        try {
            // TODO 上传到hdfs
            String res = hdfsService.uploadFileToHDFS(fileMetadata.getFileId());
            // TODO 写入数据库
            fileMetadata.setFileId(res);
            fileMetadata.setHdfsPath("/" + res);
            fileMetadataService.createNewFile(fileMetadata);
            return Result.success(res);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 文件下载
     * @param hdfsFilePath
     * @return
     * @throws IOException
     */
    @GetMapping("/download-file-from-HDFS")
    public Result downloadFileFromHDFS(
            @RequestParam(value = "hdfsFilePath", required = true) String hdfsFilePath) throws IOException {
        try {
            String res = hdfsService.downloadFileFromHDFS(hdfsFilePath);
            return Result.success(res);
        } catch (CustomException e) {
            return Result.error(e.getMessage());
        }
    }
}
